<template>
  <div>
    <BasicTable @register="registerTable">
      <template #form-advanceBefore>
        <a-button color="success" style="margin-right: 10px" @click="handleCreate">新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'material-symbols:buttons-alt',
                tooltip: '字典配置',
                onClick: handleConfig.bind(null, record),
              },
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除？',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <DictionaryModal @register="registerModal" @success="handleSuccess" />
    <DictionaryConfigDrawer @register="registerConfigModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { useWatermark } from '@/hooks/web/useWatermark';

  import { columns, searchFormSchema } from './dictionary.data';

  import { useModal } from '@/components/Modal';
  import { useDrawer } from '@/components/Drawer';
  import DictionaryModal from './DictionaryModal.vue';
  import DictionaryConfigDrawer from './DictionaryConfigDrawer.vue';

  import { deleteById, getDictionaryList } from '@/api/system/dictionary';

  import { useMessage } from '@/hooks/web/useMessage';
  import { useI18n } from '@/hooks/web/useI18n';
  import { onMounted, ref } from 'vue';

  defineOptions({ name: 'DictionaryManagement' });

  const { createMessage } = useMessage();
  const { t } = useI18n();
  const [registerModal, { openModal }] = useModal();
  const [registerConfigModal, { openDrawer: openDrawer1 }] = useDrawer();

  const app = ref(document.body);
  const { setWatermark, clear, clearAll } = useWatermark();
  const { setWatermark: setWatermark3 } = useWatermark(app, {
    // fontColor: 'red',
    // fontSize: 12,
    rotate: 40,
    // textBaseline: 'middle',
  });

  onMounted(() => {
    // setWatermark('vben');
    setWatermark3('vben admin,vue');
  });

  const [registerTable, { reload, getSelectRowKeys }] = useTable({
    // title: '开启搜索区域',
    api: getDictionaryList,
    columns,
    useSearchForm: true,
    formConfig: {
      schemas: searchFormSchema,
    },
    showTableSetting: false,
    tableSetting: { fullScreen: true },
    showIndexColumn: false,
    rowKey: 'id',
    size: 'small',
    // rowSelection: {
    //   type: 'checkbox',
    // },
    showSelectionBar: false, // 显示多选状态栏
    bordered: true,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
    },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  function handleConfig(record: Recordable) {
    openDrawer1(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    console.log(record);
    try {
      const { code, message } = await deleteById(record['id']);
      if (code != 0) {
        createMessage.error(message);
      } else {
        createMessage.success(t('sys.api.deleteSuccess'));
        reload();
      }
    } catch (err) {
      console.log(err);
    }
  }

  function handleDeleteBulk() {
    console.log(getSelectRowKeys());
  }

  function handleSuccess() {
    reload();
  }
</script>
